package client.comm.baoding.fragment

import android.content.Intent
import android.view.View
import androidx.core.view.ViewCompat
import client.comm.baoding.R
import client.comm.baoding.adapter.Td2Adapter
import client.comm.baoding.api.bean.TdBean
import client.comm.baoding.api.bean.UserInfo
import client.comm.baoding.databinding.FragmentTabteam2Binding
import client.comm.baoding.ui.TdJlJlActivity
import client.comm.baoding.ui.vm.MainViewModel
import client.comm.commlib.base.BaseFragment
import client.comm.commlib.toast.toast
import client.comm.commlib.utils.StoreUtil
import client.comm.commlib.utils.Util
import client.comm.commlib.widget.EmptyLayout
import com.blankj.utilcode.util.LogUtils

class TabTeamFragment : BaseFragment<FragmentTabteam2Binding, MainViewModel>() {

    override fun getViewId(): Int {
        return R.layout.fragment_tabteam2
    }

    override fun initViewModel(): MainViewModel = createViewModel()

    val adapter: Td2Adapter by lazy {
        Td2Adapter(requireContext(), mImgLoader)
    }

    override fun initWidget() {
        binding.statusBar.post {
            val h = Util.getStatusBarHeight(requireContext())
            val p = binding.statusBar.layoutParams
            p.height = h
            binding.statusBar.layoutParams = p
            Util.setBg(resources!!.getColor(client.comm.commlib.R.color.white), requireActivity().window)
            val controller = ViewCompat.getWindowInsetsController(binding.root)
            controller?.isAppearanceLightStatusBars = true
        }
        binding.event = this
        binding.recyclerView.adapter = adapter
//        binding.recyclerView.addItemDecoration(SpaceItemDecoration(10))
        binding.refreshLayout.setOnRefreshListener {
            adapter.hasMoreState.value = true
            viewModel.pageOne.value = 0
            viewModel.get_user_team()
        }
        binding.recyclerView.onLoadMore = {
            if (adapter.hasMoreState.value!! && !viewModel.isOneLoading.value!!) {
                viewModel.get_user_team()
            }
        }
        binding.emptyLayout.setOnClickListener {
            adapter.hasMoreState.value = true
            viewModel.pageOne.value = 0
            viewModel.get_user_team()
        }
    }

    override fun initData() {
        super.initData()

        var userId = StoreUtil.getBean<UserInfo>(requireContext())!!.id
        if (userId.isNullOrEmpty()) {
            toast("用户id不能为空")
            return
        }
        LogUtils.i("userId= $userId")
        viewModel.userId.value = userId

        adapter.hasMoreState.value = true
        viewModel.pageOne.value = 0
        viewModel.get_user_team()

        viewModel.isOneLoading.observe(this, {
            if (!it) {
                binding.refreshLayout.isRefreshing = false
            }
        })
        viewModel.callOne.observe(this, {
            it?.let {
                if (it.isSuccess) {
                    val list = it.data?.sub_list as MutableList<TdBean.Sub?>
                    if (viewModel.pageOne.value == 1) {
                        adapter.submitData(list, it.data!!)
//                        binding.title = "${it.data?.nickname}的服务中心"
                    } else
                        adapter.appendList(list)
                    if (list != null && list.size > 0) {
                        if (list.size < 10) {
                            adapter.hasMoreState.value = false
                        }
                        binding.emptyLayout.visibility = View.GONE
                    } else {
                        if (adapter.itemCount == 0)
                            binding.emptyLayout.errorType = EmptyLayout.NODATA
                        adapter.hasMoreState.value = false
                    }
                } else {
                    if (adapter.itemCount == 0)
                        binding.emptyLayout.errorType = EmptyLayout.NETWORK_ERROR
                }
            }
        })
    }


    /**
     * 奖励记录
     */
    fun jljl() {
        startActivity(Intent(context, TdJlJlActivity::class.java))
    }

}